package cn.sc.allinpay.allfavour.business.checkscan;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.base.activity.BaseActivity;
import cn.sc.allinpay.allfavour.business.checktrans.CheckTransActivity;


/**
 * activity 验券扫码
 * author：pany
 * on 2017/12/1 11:14
 */
public class CheckScanActivity extends BaseActivity<CheckScanContract.View, CheckScanContract.Presenter>
        implements CheckScanContract.View, DecoratedBarcodeView.TorchListener {

    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private boolean isLightOpen = false;

    @BindView(R.id.switch_flashlight)
    AppCompatImageButton switch_flashlight;
    @BindView(R.id.zxing_status_view)
    TextView zxing_status_view;


    @Override
    protected int layoutId() {
        return R.layout.activity_check_scan;
    }

    @Override
    protected void layoutTitle(View top, AppCompatTextView left, AppCompatTextView title, AppCompatTextView right) {
        title.setText("验券");
    }

    @Override
    protected CheckScanContract.Presenter bindPresenter() {
        return new CheckScanPresenter();
    }

    @Override
    protected void layoutContent(Bundle savedInstanceState) {
        zxing_status_view.setText("请扫描券号");
        barcodeScannerView = findViewById(R.id.zxing_barcode_scanner);
        barcodeScannerView.setTorchListener(this);

        // if the device does not have flashlight in its camera,
        // then remove the switch flashlight button...
        if (!hasFlash()) {
            switch_flashlight.setVisibility(View.GONE);
        }

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    /**
     * Check if the device's camera has a Flashlight.
     *
     * @return true if there is Flashlight, otherwise false.
     */
    private boolean hasFlash() {
        return getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    public void switchFlashlight(View view) {
        if (isLightOpen) {
            isLightOpen = false;
            barcodeScannerView.setTorchOff();
        } else {
            isLightOpen = true;
            barcodeScannerView.setTorchOn();
        }
    }

    @Override
    public void onTorchOn() {

    }

    @Override
    public void onTorchOff() {
    }

    @OnClick(R.id.btnFlow)
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnFlow:
                startActivity(new Intent(this, CheckTransActivity.class));
                break;
        }
    }
}
